package com.qiandw.enterprise.credit.service

import java.util

import com.qiandw.enterprise.credit.entity.Module
import com.qiandw.enterprise.credit.mapper.ModuleMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

import scala.collection.JavaConversions._

/**
  * Created by Draven on 2018/7/9.
  * Email:hjx@qiandw.com
  */
@Service
class ModuleSrvImpl extends ModuleSrv {

  @Autowired
  var moduleMapper: ModuleMapper = _

  override def list1LevelModules(): util.List[Module] = {
    val modules = moduleMapper.listModulesByParentId(1)
    val sortedModules= modules.filter(_.level == 1).sortBy(_.seq)
    sortedModules.foreach(m => {
      val child = moduleMapper.listModulesByParentId(m.id).sortBy(_.seq)
      m.child = child
    })
    sortedModules
  }

  override def listAllModules(): util.List[Module] = {
    val modules = moduleMapper.selectAll().sortBy(_.seq)
    modules
  }

  override def addModule(module: Module): Int = {
    val parent = moduleMapper.selectByPrimaryKey(module.parentId)
    module.level = parent.level + 1
    val res = moduleMapper.insertSelective(module)
    res
  }

  override def removeModule(id: Integer): Int = {
    val res = moduleMapper.deleteByPrimaryKey(id)
    res
  }

  override def updateModule(module: Module): Int = {
    val res = moduleMapper.updateByPrimaryKeySelective(module)
    res
  }
}
